// ActionScript file

import arborescence.FilteredTreeDataDescriptor;

import flash.events.Event;
import flash.events.MouseEvent;
import flash.media.Sound;
import flash.media.SoundChannel;
import flash.net.URLLoader;
import flash.net.URLLoaderDataFormat;
import flash.net.URLRequest;
import flash.xml.XMLDocument;
import flash.xml.XMLNode;
import flash.xml.XMLNodeType;

import flashx.textLayout.conversion.TextConverter;

import mx.collections.ArrayCollection;
import mx.collections.ICollectionView;
import mx.collections.XMLListCollection;
import mx.controls.Image;
import mx.controls.Text;
import mx.controls.TextArea;

import org.osmf.events.MediaPlayerStateChangeEvent;
import org.osmf.events.TimeEvent;

import spark.components.Button;
import spark.components.RichEditableText;
import spark.components.RichText;
import spark.components.TextArea;
import spark.components.VideoPlayer;



/**
* Chargement du fichier xml dynamiquement dans le tree
*/
public function clikilik(evt:Event):void
{
	var myXML:XML = new XML();
	myXML.ignoreWhite = true;				 
	var XML_URL:String = xmlinput.text;
	var myXMLURL:URLRequest = new URLRequest(XML_URL);
	var myLoader:URLLoader = new URLLoader(myXMLURL);
	myLoader.addEventListener("complete", xmlLoaded);
	
	
	function xmlLoaded(event:Event):void
	{
		myXML = XML(myLoader.data);
		
		var treeDataLoad:XMLList = XMLList(myXML.children());
		createCheckBox(treeDataLoad);
		
		tree.dataProvider=treeDataLoad;
		tree.dataDescriptor= new FilteredTreeDataDescriptor(filterFunc);
		
	}
	PopUpManager.removePopUp(_window);
	tree.validateNow();
	loadbutton.enabled=true;
	monpanel.title = monpaneltitre +" :: "+ XML_URL.substr(XML_URL.lastIndexOf("/")+1,XML_URL.length-1);
	
	
}
public var monpaneltitre:String="Arborescence Multimedia";
/**
* Redimensionne l'affichage du media
*/
public function resizeContenu():void{
	if (this.affichage.numChildren!=0){
		if(affichage.numElements > 0){
			if(affichage.numElements>0){
				affichage.getElementAt(0).width=affichage.width;
				affichage.getElementAt(0).height=affichage.height;
				
			}
		}
	}
}


/**
 * Creation des checkbox des types et leur listener
*/
public function createCheckBox( arbreTemp:XMLList):void{
	
	types.removeAllElements();
	var nostypes:ArrayCollection = new ArrayCollection();
	var cb:mx.controls.CheckBox;
	
	for each(var t:XML in arbreTemp..fichier.@type){
		if (!nostypes.contains(t.toString())){
			nostypes.addItem(t.toString());
			cb= new mx.controls.CheckBox();
			cb.label = t.toString();
			cb.id=t.toString().toLowerCase();
			cb.selected = true;
			cb.addEventListener(Event.CHANGE, changementEtat);
			types.addElement(cb);
		}
		
	}
	
}

private function changementEtat(event:Event):void
{
	tree.invalidateList();
}



/****************************************************************/
/********************AFFICHAGE DES MEDIAS************************/
/****************************************************************/
protected function vpCompleteHandler(event:TimeEvent):void {}

protected function vpMediaPlayerStateChangeHandler(event:MediaPlayerStateChangeEvent):void {}

public function affVideo(source:String):void{
	var vid:VideoPlayer = new VideoPlayer();
	vid.source= source;
	vid.width = affichage.width;
	vid.height = affichage.height;
	vid.loop = false;
	this.affichage.addElementAt(vid,0);
}

public function affImg(source:String):void{
	var img:mx.controls.Image = new mx.controls.Image();
	img.source = source;
	img.width = affichage.width;
	img.height = affichage.height;
	this.affichage.addElementAt(img,0);
}


public var mySound:Sound ;
public var audio:SoundChannel = new SoundChannel();


public function affSon(source:String):void{
	
	
	mySound = new Sound(new URLRequest(source));
	audio = mySound.play(0,1000);
	
	var boutonS :mx.controls.Button = new mx.controls.Button();
	boutonS.label = "stop";
	boutonS.height = 40;
	boutonS.width = 80;
	boutonS.addEventListener(MouseEvent.CLICK, stopMusiqueEvent);

	affichage.addElementAt(boutonS,0);
}        

private function stopMusique():void
{
	audio.stop();
	mySound = null;     
}


private function stopMusiqueEvent(event:MouseEvent):void
{
	audio.stop();
	mySound = null;     
}



private function affTexte(source:String):void {
	var PATH:String = source;
	
	
	var urlRequest:URLRequest = new URLRequest(PATH);
	var urlLoader:URLLoader = new URLLoader();
	urlLoader.dataFormat = URLLoaderDataFormat.TEXT; // default
	urlLoader.addEventListener(Event.COMPLETE, urlLoader_complete);
	urlLoader.load(urlRequest);
	var myText:spark.components.TextArea = new spark.components.TextArea();
	myText.editable=false;
	function urlLoader_complete(evt:Event):void {
		
		if(PATH.search("html")!=-1){
			myText.textFlow = TextConverter.importToFlow(urlLoader.data, TextConverter.TEXT_FIELD_HTML_FORMAT);
		}else{
			myText.text = urlLoader.data;
		}
	}
	affichage.addElementAt(myText,0);
	
	resizeContenu();


}

